package com.wnhz.work.jy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.wnhz.work.jy.R;
import com.wnhz.work.jy.bean.AssetRecordBean;

import java.util.List;

/**
 * Created by Administrator on 2016/11/7 0007.
 */
public class AssetRecordAdapter extends BaseAdapter {
    private Context context;
    private List<AssetRecordBean> list;
    private AssetItemAdapter mAdapter;
    private View.OnClickListener listener;

    public AssetRecordAdapter(Context context, List<AssetRecordBean> list) {
        this.context = context;
        this.list = list;
    }

    public View.OnClickListener getListener() {
        return listener;
    }

    public void setListener(View.OnClickListener listener) {
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        AssetRecordViewHolder viewHolder = new AssetRecordViewHolder();
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.asset_record_item, null);
            viewHolder.orderNumber = (TextView) convertView.findViewById(R.id.tv_order_number);
            viewHolder.type = (TextView) convertView.findViewById(R.id.tv_type);
            viewHolder.num = (TextView) convertView.findViewById(R.id.tv_number);
            viewHolder.allPrice = (TextView) convertView.findViewById(R.id.tv_all_price);
            viewHolder.listView = (ListView) convertView.findViewById(R.id.lv_asset_item);
            viewHolder.btn = (TextView) convertView.findViewById(R.id.btn);
            viewHolder.lay = convertView.findViewById(R.id.lay);
            viewHolder.onclick = convertView.findViewById(R.id.onclick);
            viewHolder.text3 = convertView.findViewById(R.id.text3);
            viewHolder.btn_accomplish = (TextView) convertView.findViewById(R.id.btn_accomplish);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (AssetRecordViewHolder) convertView.getTag();
        }

        viewHolder.orderNumber.setText("订单号：" + list.get(position).getOrderNo());

        viewHolder.num.setText("共" + list.get(position).getTotalNum() + "件");
        viewHolder.allPrice.setText("¥ " + list.get(position).getPayFee());

        mAdapter = new AssetItemAdapter(context, list.get(position).getOrderDetail());
        viewHolder.listView.setAdapter(mAdapter);
        if (listener != null){
            viewHolder.btn.setTag(position);
            viewHolder.btn_accomplish.setTag(position);
            viewHolder.btn.setOnClickListener(listener);
            viewHolder.btn_accomplish.setOnClickListener(listener);
            viewHolder.lay.setOnClickListener(listener);
            viewHolder.lay.setTag(position);
            viewHolder.onclick.setOnClickListener(listener);
            viewHolder.onclick.setTag(position);
        }
        if (list.get(position).getMyType() == 1){
            viewHolder.type.setText("存金库订单");
            viewHolder.text3.setVisibility(View.VISIBLE);
            viewHolder.allPrice.setVisibility(View.VISIBLE);
            viewHolder.btn.setVisibility(View.GONE);
            viewHolder.btn_accomplish.setVisibility(View.GONE);

        } else if (list.get(position).getMyType() == 2){
            viewHolder.text3.setVisibility(View.GONE);
            viewHolder.allPrice.setVisibility(View.GONE);
            viewHolder.btn.setVisibility(View.VISIBLE);
            viewHolder.btn_accomplish.setVisibility(View.GONE);
            viewHolder.num.setText("共" + list.get(position).getOrderDetail().get(0).getGoodsNums() + "件");
            viewHolder.type.setText("卖金币订单");
            sellGlodStart(viewHolder,position);
        } else if (list.get(position).getMyType() == 3){
            viewHolder.allPrice.setVisibility(View.GONE);
            viewHolder.text3.setVisibility(View.GONE);
            viewHolder.btn.setVisibility(View.GONE);
            viewHolder.btn_accomplish.setVisibility(View.VISIBLE);
            viewHolder.num.setText("共" + list.get(position).getOrderDetail().get(0).getGoodsNums() + "件");
            viewHolder.type.setText("提金币订单");
            takeGlodStart(viewHolder,position);
        }


        return convertView;
    }

    private void sellGlodStart(AssetRecordViewHolder viewHolder,int position){
        String start = list.get(position).getOrderState();
        if (start != null && !"".equals(start)){

        }else if ("1".equals(start))
            viewHolder.btn.setText("待打款");
        else if ("2".equals(start))
            viewHolder.btn.setText("已打款");
    }

    private void takeGlodStart(AssetRecordViewHolder viewHolder,int position){
        String start = list.get(position).getOrderState();
        StringBuilder  string = new StringBuilder();
        if (start != null && !"".equals(start)){
            switch (Integer.parseInt(start)){
                case 0:
                    string .append("待付款");
                    break;
                case 1:
                    string .append("待发货");
                    break;
                case 2:
                    string .append("待收货");
                    break;
                case 3:
                    string .append("待评价");
                    break;
                case 4:
                    string .append("待签收");
                    break;
                case 5:
                    string .append("取消订单");
                    break;
                case 6:
                    string .append("退款");
                    break;
                default:
                    break;
            }
        }
        viewHolder.btn_accomplish.setText(string);
    }

    class AssetRecordViewHolder {
        private TextView orderNumber;
        private TextView type;
        private ListView listView;
        private TextView num;
        private TextView allPrice;
        TextView btn,btn_accomplish;
        View text3,lay,onclick;
    }
}
